/*
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2008.
 *
 * Licensed under the Aduna BSD-style license.
 */
package edu.tju.rdfbase.store.helpers;

import java.util.ArrayList;
import java.util.List;

import edu.tju.rdfbase.model.Statement;
import edu.tju.rdfbase.store.NotifyingStoreConnection;
import edu.tju.rdfbase.store.StoreConnectionListener;

/**
 * This class extends {@link StoreConnectionBase} adding support for
 * {@link NotifyingStoreConnection}. implementations.
 * 
 * @author Arjohn Kampman
 * @author jeen
 * @author James Leigh
 */
public abstract class NotifyingStoreConnectionBase extends StoreConnectionBase implements
		NotifyingStoreConnection
{

	/*-----------*
	 * Variables *
	 *-----------*/

	private final List<StoreConnectionListener> listeners = new ArrayList<StoreConnectionListener>(0);

	/*---------*
	 * Methods *
	 *---------*/

	public void addConnectionListener(StoreConnectionListener listener) {
		synchronized (listeners) {
			listeners.add(listener);
		}
	}

	public void removeConnectionListener(StoreConnectionListener listener) {
		synchronized (listeners) {
			listeners.remove(listener);
		}
	}

	protected boolean hasConnectionListeners() {
		synchronized (listeners) {
			return !listeners.isEmpty();
		}
	}

	protected void notifyStatementAdded(Statement st) {
		synchronized (listeners) {
			for (StoreConnectionListener listener : listeners) {
				listener.statementAdded(st);
			}
		}
	}

	protected void notifyStatementRemoved(Statement st) {
		synchronized (listeners) {
			for (StoreConnectionListener listener : listeners) {
				listener.statementRemoved(st);
			}
		}
	}
}
